<template>
  <div class="login">
    <div class="box">
      <div class="logo">
        <img src="@/assets/common/login-logo.png" alt="">
      </div>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" type="password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item>
          <el-button class="btn" @click="submit">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      rules: {
        mobile: [
          { requuired: true, message: '必填', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              const reg =
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              reg.test(value)
                ? callback()
                : callback(new Error('请正确输入手机号'))
            },
            trigger: 'change'
          }
        ],
        password: [
          { requuired: true, message: '必填', trigger: 'change' },
          {
            min: 6,
            max: 12,
            message: '请输入六到十二位密码',
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    submit() {
      console.log(1)
      this.$refs.form.validate(async(result) => {
        if (result) {
        await  this.$store.dispatch('user/isLogin',this.form)
        this.$message.success('登录成功')
        const _redirect = this.$route.query.redirect || '/'
        console.log('_redirect',_redirect);
        this.$router.push(_redirect)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background: url('~@/assets/common/login.jpg');
  display: flex;
  justify-content: center;
  align-items: center;
  .box {
    width: 520px;
    height: 400px;
    .logo {
      text-align: center;
      padding-bottom: 20px;
    }
    .btn {
      width: 100%;
      color: #fff;
      background-color: #4080fe;
      border: none;
    }
  }
}
</style>
